package com.qmhl.toolposition.activity

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.hjq.toast.Toaster
import com.lxj.xpopup.XPopup
import com.qmhl.toolposition.api.UserService
import com.qmhl.toolposition.base.BaseActivity
import com.qmhl.toolposition.databinding.ActivityPositionBinding
import com.qmhl.toolposition.event.LoginEvent
import com.qmhl.toolposition.event.TabSelectEvent
import com.qmhl.toolposition.utils.PUtils
import com.qmhl.toolposition.utils.PreferenceUtil
import com.qmhl.toolposition.utils.RegexUtil
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus


/**
 * @author: lqhl
 * @createTime: 2023-11-04  15:25
 * @description: 添加定位
 */
class AddPositionActivity : BaseActivity() {

    lateinit var binding: ActivityPositionBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPositionBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }


    override fun initData() {
        super.initData()
    }


    override fun initListener() {
        super.initListener()
        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.llPhone.setOnClickListener {
            var currentUser = PreferenceUtil.getCurrentUser()
            if (!currentUser.isLogin) {
                EventBus.getDefault().postSticky(LoginEvent(0))
            } else {

                addFromPhone()
            }
        }

        binding.llWechat.setOnClickListener {
            var currentUser = PreferenceUtil.getCurrentUser()
            if (!currentUser.isLogin) {

                EventBus.getDefault().postSticky(LoginEvent(0))
            } else {

                addFromWechat()
            }
        }
    }

    private fun addFromWechat() {
        lifecycleScope.launch {
            var content = UserService.create()
                .isVip()
            if (content.code == 200) {
                val isVip = content.data
                if (isVip) {
                    showAddWechatDialog()
                } else {
                    val intent = Intent(this@AddPositionActivity, VipActivity::class.java)
                    startActivity(intent)
                    finish()
                }
            } else {
                Toaster.showShort(content.msg)
            }
        }
    }

    private fun addFromPhone() {

        lifecycleScope.launch {
            var content = UserService.create()
                .isVip()
            if (content.code == 200) {
                val isVip = content.data
                if (isVip) {
                    showAddFriendDialog()
                } else {
                    val intent = Intent(this@AddPositionActivity, VipActivity::class.java)
                    startActivity(intent)
                    finish()
                }
            } else {
                Toaster.showShort(content.msg)
            }
        }


    }

    private fun showAddFriendDialog() {
        XPopup.Builder(this)
            .autoFocusEditText(false)
            .hasBlurBg(true)
            .asInputConfirm(
                "添加好友", "请输入您要定位的手机号码"
            ) {
                addFriend(it)
            }
            .show()
    }


    private fun showAddWechatDialog() {
        val web: UMWeb =
            UMWeb("https://oos-cn.ctyunapi.cn/position/position_sign_jiagu.apk?Signature=bXoyeW1dfoGl/YWVWuLPkWMOOlo%3D&AWSAccessKeyId=builtIn%7C3904097959380979947&Expires=1701702798")
        web.title = "爱守护足迹" //标题
        val image: UMImage = UMImage(
            this,
            "https://oos-cn.ctyunapi.cn/position/banner1.png?Signature=rC7RRggpVfdFNKd/LFUB/6bYu8E%3D&AWSAccessKeyId=builtIn%7C3904097959380979947&Expires=1731081939&r=4344"
        ) //网络图片

        web.setThumb(image) //缩略图

        web.description = "好友邀请您下载" //描述


        ShareAction(this)
            .withMedia(web)
            .setCallback(object : UMShareListener {
                override fun onStart(p0: SHARE_MEDIA?) {

                }

                override fun onResult(p0: SHARE_MEDIA?) {

                }

                override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {

                }

                override fun onCancel(p0: SHARE_MEDIA?) {

                }
            })
            .setDisplayList(
                SHARE_MEDIA.WEIXIN,
                SHARE_MEDIA.WEIXIN_CIRCLE,
                SHARE_MEDIA.WEIXIN_FAVORITE
            )
            .open()
    }


    private fun addFriend(phone: String?) {
        if (phone == null) {
            Toaster.showShort(PUtils.Toast.PLEASE_INPUT_PHONE)
            return
        }
        var regexPhone = RegexUtil.regexPhone(phone)
        if (!regexPhone) {
            Toaster.showShort(PUtils.Toast.PLEASE_INPUT_PHONE)
        }

        lifecycleScope.launch {
            var content = UserService.create()
                .addFriend(phone)
            if (content.code == 200) {
                Toaster.showShort(PUtils.Toast.SEND_INVITE_SUCCESS)
            } else {
                Toaster.showShort(content.msg)
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}